﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class sentEcard : System.Web.UI.Page
{
    private bussiness bus;
    string id;
    static List<listEmail> lstEmail = new List<listEmail>();
    protected void Page_Load(object sender, EventArgs e)
    {
        id = Request.QueryString["id"];
        if (!IsPostBack)
        {
            if (Session["customer"] != null)
            {
                Customer c = (Customer)Session["customer"];
                txtNameSender.Text = c.Name;
                txtEmailSender.Text = c.Email;
            }
            if (Session["sent_details"] != null)
            {
                SentEcard se = (SentEcard)Session["sent_details"];
                txtTitle.Text = se.Title;
                txtMessage.Text = se.Massage;
                txtSignature.Text = se.Signature;
            }
            GridView1.DataSource = lstEmail;
            GridView1.DataBind();
            txtDateSent.Text = string.Format("{0:MM/dd/yyyy}", DateTime.Now);
            bus = new bussiness();

            List<Ecard> lstEcard = bus.getAllEcard();
            List<Ecard> lstEcard_display = new List<Ecard>();
            foreach (Ecard ecard in lstEcard)
            {
                if (ecard.EcardId == Convert.ToInt32(id))
                {
                    lstEcard_display.Add(ecard);
                    Repeater1.DataSource = lstEcard_display;
                    Repeater1.DataBind();
                    break;
                }
            }
        }

    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        DateTime date = Calendar1.SelectedDate;
        txtDateSent.Text = string.Format("{0:MM/dd/yyyy}", date);

    }
    private void loadData()
    {
        GridView1.DataSource = lstEmail;
        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
        int index = gvRow.RowIndex;
        lstEmail.RemoveAt(index);
        loadData();
    }
    protected void btnAddReceiver_Click(object sender, EventArgs e)
    {
        listEmail le = new listEmail();
        le.Name = txtNameReceiver.Text;
        le.Email = txtEmailReceiver.Text;
        bool flag = true;
        foreach (listEmail l in lstEmail)
        {
            if (le.Email == l.Email)
            {
                flag = false;
                break;
            }
        }
        if (flag)
        {
            lblInfo.Text = "";
            lstEmail.Add(le);
            loadData();
        }
        else
        {
            lblInfo.Text = "Email is existed";
        }


    }
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.Date < (System.DateTime.Now.Date))
        {

            e.Day.IsSelectable = false;
            e.Cell.Font.Strikeout = true;

        }
    }
    protected void btnReview_Click(object sender, EventArgs e)
    {
        SentEcard se = new SentEcard();
        se.EcardId = Convert.ToInt32(id);
        se.Title = txtTitle.Text;
        se.Massage = txtMessage.Text;
        se.Signature = txtSignature.Text;
        Session["sent_details"] = se;
        string url = "viewCardDetails.aspx?id=" + id + "";
        Response.Redirect(url);
    }
    protected void btnSent_Click(object sender, EventArgs e)
    {
        Customer c = (Customer)Session["customer"];
        bus = new bussiness();
        if (lstEmail.Count > 0)
        {
            SentEcard se = new SentEcard();
            se.CustomerId = c.CustomerId;
            se.EcardId = Convert.ToInt32(id);
            se.DateSent = Convert.ToDateTime(txtDateSent.Text);
            se.Title = txtTitle.Text;
            se.Massage = txtMessage.Text;
            se.Signature = txtSignature.Text;
            bus.insertSentEcard(se);
            int sentEcardId = bus.getSentEcardId(c.CustomerId);
            foreach (listEmail le in lstEmail)
            {
                le.SentEcardId = sentEcardId;
                bus.insertListEmail(le);
            }
            SmtpClient smtpClient;
            MailMessage message;
            if (se.DateSent.Day == DateTime.Now.Day && se.DateSent.Month == DateTime.Now.Month && se.DateSent.Year == DateTime.Now.Year)
            {
                smtpClient = new SmtpClient("smtp.gmail.com", 587);
                smtpClient.EnableSsl = true;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.UseDefaultCredentials = true;
                smtpClient.Credentials = new NetworkCredential("nguoiban1234567@gmail.com", "vanthanh");
               
                //Message object must be disposed before deleting temp attachment files
                {
                    foreach (listEmail le in lstEmail)
                    {
                        message = new MailMessage();
                        message.To.Add(le.Email);
                        LogSentMail log = new LogSentMail();
                        log.CustomerId = c.CustomerId;
                        log.NameSender = c.Name;
                        log.EmailSender = c.Email;
                        log.EcardId = Convert.ToInt32(id);
                        log.DateSent = Convert.ToDateTime(txtDateSent.Text);
                        log.Title = txtTitle.Text;
                        log.Messages = txtMessage.Text;
                        log.Signatures = txtSignature.Text;
                        log.EmailReceived =le.Email;
                        log.NameReceived = le.Name;

                        bus.insertLogSentMail(log);
                        int logSentMailId = bus.getLogSentMailId(c.CustomerId);

                        message.From = new MailAddress("nguoiban1234567@gmail.com", "ProjectSem3");
                        message.Subject = c.Email + " has been sent for you one Ecard";
                        message.Body = c.Name + " [" + c.Email + "] just sent you an ecard from Ecard.com <br/>"
                            + "You can view it by clicking here: <br/>"
                            + "<a href =" + "http://localhost/Eproject_sem3_thanh/viewcardsent.aspx?id=" + logSentMailId + ">" + "http://localhost/Eproject_sem3_thanh/viewcardsent.aspx?id=" + logSentMailId + "</a>";
                        message.IsBodyHtml = true;
                       
                        try
                        {
                            smtpClient.Send(message);
                          
                            //xong
                        }
                        catch
                        {
                            // lỗi
                        }
                    }
                    bus.updateCountEcard(Convert.ToInt32(id));
                    lstEmail = new List<listEmail>();
                    Response.Redirect("thankyou.aspx");
                }
            }
            else
            {
                Response.Redirect("thankyou.aspx");
            }
        }
        else
        {
            lblInfo.Text = "you must add email receiver";
        }
    }
}